 @include "query.awk"
 function find(query,found,at,active,   init) { 
   array(init) 
   findand(query,at,init,found,active,1)
   return length(found)
 }
 function findand(query,at,before,found,active,nth,   \
                  col,now,new) {
   for(col in query) {
     if (! ats(col,query[col],at,now,active))
       return array(found) 
     delete query[col]
     if (nth==1)
       return findand(query,at,now,found,active, ++nth)
     else
       if (findIntersect(before,now, new))
         return findand(query,at,new,found,active, ++nth) 
   }
   return asort(before,found,"valdesc")
 }
 function findIntersect(before,now,new,   i,n,tmp) {
   array(new)
   for(i in before) 
     tmp[before[i]["id"]] = before[i]["val"]
   for(i in now)
     if (i in tmp) {
       n++
       new[n]["id"]  = i
       new[n]["val"] = tmp[i] + now[i]["val"]
     }
   return n>0
 }

